package cc.admin.modules.graduate.service.impl;

import cc.admin.modules.graduate.entity.Topic;
import cc.admin.modules.graduate.mapper.TopicMapper;
import cc.admin.modules.graduate.service.ITopicService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.List;

/**
 * @Description: 话题树
 * @Author: cc-admin
 * @Date:   2025-02-18
 * @Version: V1.0.0
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements ITopicService {

    @Autowired
    private TopicMapper topicMapper;

    @Override
    public List<Topic> getTopicTree() {
        LambdaQueryWrapper<Topic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(Topic::getParentId).or().eq(Topic::getParentId, "");
        queryWrapper.orderByAsc(Topic::getValue);
        List<Topic> topics = topicMapper.selectList(queryWrapper);
        topics.forEach(topic -> {
            LambdaQueryWrapper<Topic> queryWrapper2 = new LambdaQueryWrapper<>();
            queryWrapper2.eq(Topic::getParentId, topic.getValue());
            queryWrapper2.orderByAsc(Topic::getValue);
            List<Topic> treeNode = topicMapper.selectList(queryWrapper2);
            topic.setChildren(treeNode);
        });
        return topics;
    }
}
